package com.shadt.cloud.controller;

import java.util.Timer;
import java.util.TimerTask;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shadt.cloud.service.SMSService;
import com.shadt.core.controller.BaseController;
import com.shadt.core.model.Json;
import com.shadt.sms.entity.SysSmsRecord;

/**
 * 短信控制器
 */
@Controller
@RequestMapping(value="/sms")
public class SMSController extends BaseController{

	@Autowired
	SMSService service;
	/**
	 * 发送验证码
	 * @param phone
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/send",method={RequestMethod.GET})
	public Json send(String phone){
		Json j = new Json();
		if("".equals(phone)||null==phone){
			j.setMsg("手机号码不能为空！");
			j.setSuccess(false);
		}else{
			try {
				SysSmsRecord r = service.send(phone);
				if(r!=null){
					final Long id= r.getId();
					j.setSuccess(true);
					j.setMsg("成功发送验证码！");
					new Timer().schedule(new TimerTask(){
						public void run() {
							try {
								service.invalid(id);
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					}, 1*60*1000);// 设定指定的时间,此处为15分钟
				} 
			} catch (Exception e) {
				j.setMsg("发送验证码接口异常！");
				e.printStackTrace();
			}
		}
		return j;
	}

}
